package UI;

import domain.PhoneAddress;
import servers.PhoneAddressServers;
import utils.JDBCutils;

import java.util.ArrayList;
import java.util.Scanner;

public class PhoneAddressUI {
    Scanner sc=new Scanner(System.in);
    PhoneAddressServers pas=new PhoneAddressServers();
    public String functionchoice(){
        System.out.println("请输入你选择的方法");
        System.out.println("1.增加");
        System.out.println("2.删除");
        System.out.println("3.修改");
        System.out.println("4.查看所有");
        System.out.println("5.退出");
        System.out.println("请输入1~5：");
        String choice=sc.next();
        return choice;
    }
    public void function(String choice){
        switch (choice){
            case "1":
                add();
                break;
            case "2":
                delete();
                break;
            case "3":
                update();
                break;
            case "4":
                select();
                break;
            case "5":
                JDBCutils.colse();
                System.exit(0);
                break;
            default:break;
        }
    }

    private void update() {
        System.out.println("修改界面");
        System.out.println("请输入修改人的姓名");
        String name=sc.next();
        if(pas.selectOnename(name).getName()!=null){
            System.out.println("查看原信息");
            System.out.println(pas.selectOnename(name));
            int id=pas.selectOnename(name).getId();
            System.out.println("请输入新的名字");
             name=sc.next();
            System.out.println("请输入新的手机号");
            String phonenum=sc.next();
            System.out.println("请输入新的位置");
            String position=sc.next();
            System.out.println("请输入新的单位");
            String company=sc.next();
            PhoneAddress p=new PhoneAddress(id,name,phonenum,position,company);
            if(pas.updatePhone(p)){
                System.out.println("修改成功");
            }else{
                System.out.println("修改失败");
            }
        }else{
            System.out.println("修改人不存在");
        }
    }

    private void select() {
        ArrayList<PhoneAddress> arr=pas.selectAll();
        for(PhoneAddress s:arr){
            System.out.println(s);
        }
    }

    private void delete() {
        System.out.println("输入需要删除的姓名:");
        String name=sc.next();
        if(pas.deletePhone(name)){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

    private void add() {
        System.out.println("请输入姓名");
        String name=sc.next();
        System.out.println("请输入手机号");
        String phone=sc.next();
        System.out.println("请输入位置");
        String position=sc.next();
        System.out.println("请输入公司");
        String company=sc.next();
        PhoneAddress p=new PhoneAddress(name,phone,position,company);
        if(pas.addPhone(p)){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

}
